package com.ybkj.o2o.zhuanxian.service.impl;

import com.ybkj.o2o.zhuanxian.entity.Line;
import com.ybkj.o2o.zhuanxian.mapper.LineMapper;
import com.ybkj.o2o.zhuanxian.service.LineService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DefaultLineService implements LineService {

  @Autowired
  private LineMapper lineMapper;

  @Override
  public Line create(Line line) {
    lineMapper.insert(line);
    return line;
  }

  @Override
  public Line findById(Long id, String appKey) {

    return lineMapper.findById(id, appKey);
  }

  @Override
  public int deleteLine(Long id, String appKey) {
    return lineMapper.deleteLine(id, appKey);
  }

  @Override
  public List<Line> findByParam(Long companyId, String companyName, String name, String startCity,
      String startAddress, String endCity, String endAddress, Integer limit, Integer rows,
      String appKey) {
    return lineMapper.findByParam(companyId, StringUtils.isNotBlank(companyName) ? "%"
        + companyName + "%" : null, StringUtils.isNotBlank(name) ? "%" + name + "%" : null,
        StringUtils.isNotBlank(startCity) ? "%" + startCity + "%" : null,
        StringUtils.isNotBlank(startAddress) ? "%" + startAddress + "%" : null,
        StringUtils.isNotBlank(endCity) ? "%" + endCity + "%" : null,
        StringUtils.isNotBlank(endAddress) ? "%" + endAddress + "%" : null, limit, rows, appKey);
  }
  
  @Override
  public List<Line> findLineByParams(
      String keyWord, Integer limit, Integer rows, String appKey) {
    if (StringUtils.isNotBlank(keyWord)) {
      keyWord = "%" + keyWord + "%";
    }
    return lineMapper.findLineByParams(keyWord, limit, rows, appKey);
  }
  
  @Override
  public List<Line> findEndByStart(
	      String startCity, String startAddress, Integer limit, Integer rows, String appKey){
	  if (StringUtils.isNotBlank(startCity)) {
		  startCity = "%-" + startCity + "%";
	  }
	  return lineMapper.findEndByStart(startCity, startAddress, limit, rows, appKey);
  }
  
  
  @Override
  public Long countLineByParams(String keyWord, String appKey) {
    if (StringUtils.isNotBlank(keyWord)) {
      keyWord = "%" + keyWord + "%";
    }
    return lineMapper.countLineByParams(keyWord, appKey);
  }
  
  public Long countLineByStart(String startCity, String startAddress, String appKey) {
	  if (StringUtils.isNotBlank(startCity)) {
		  startCity = "%-" + startCity + "%";
	    }
	    return lineMapper.countLineByStart(startCity, startAddress, appKey);
  }

  @Override
  public Long countByParam(Long companyId, String companyName, String name, String startCity,
      String startAddress, String endCity, String endAddress, String appKey) {
    return lineMapper.countByParam(companyId, StringUtils.isNotBlank(companyName) ? "%"
        + companyName + "%" : null, StringUtils.isNotBlank(name) ? "%" + name + "%" : null,
        StringUtils.isNotBlank(startCity) ? "%" + startCity + "%" : null,
        StringUtils.isNotBlank(startAddress) ? "%" + startAddress + "%" : null,
        StringUtils.isNotBlank(endCity) ? "%" + endCity + "%" : null,
        StringUtils.isNotBlank(endAddress) ? "%" + endAddress + "%" : null, appKey);
  }

  @Override
  public int update(Line line) {
    return lineMapper.update(line);
  }

  @Override
  public List<Line> findByIds(List<Long> lineIds) {

    return lineMapper.findByIds(lineIds);
  }

  public List<Line> findAll(String city, String appKey) {
    return lineMapper.findAll(StringUtils.isNotBlank(city) ? "%" + city + "%" : null, appKey);

  }

  @Override
  public List<Line> findLineByCompany(Long companyId, String city, String appKey) {

    return lineMapper.findLineByCompany(companyId, StringUtils.isNotBlank(city) ? "%" + city + "%"
        : null, appKey);
  }

  @Override
  public List<Line> findByCityAndAddress(Long companyId, String startCity, String startAddress,
      String endCity, String endAddress) {
    return lineMapper.findByCityAndAddress(companyId, startCity,
        StringUtils.isNotBlank(startAddress) ? "%" + startAddress + "%" : null, endCity,
        StringUtils.isNotBlank(endAddress) ? "%" + endAddress + "%" : null);
  }

  @Override
  public List<Line> findStartEndByLine(String startCity, String endCity, String appKey) {
    return lineMapper.findStartEndByLine(StringUtils.isNotBlank(startCity) ? "%" + startCity + "%"
        : null, StringUtils.isNotBlank(endCity) ? "%" + endCity + "%" : null, appKey);
  }

  @Override
  public List<Line> findByCompany(Long companyId, String appKey) {
    return lineMapper.findByCompany(companyId, appKey);
  }

  @Override
  public List<Line> queryLineByStartCity(String startCity, String appKey) {

    return lineMapper.queryLineByStartCity(
        StringUtils.isNotBlank(startCity) ? startCity : null,appKey);
  }

  @Override
  public List<Line> queryEndLineBySatrt(String startCity, String startAddress,
      String endCity ,String appKey) {
    return lineMapper.queryEndLineBySatrt(
        StringUtils.isNotBlank(startCity) ? "%" + startCity + "%" : null,
        StringUtils.isNotBlank(startAddress) ? "%" + startAddress + "%" : null,
        StringUtils.isNotBlank(endCity) ? "%" + endCity + "%" : null,appKey);
  }

  @Override
  public Long countByAddress(Long companyId, String startCity, String startAddress,
      String endCity, String endAddress,String appKey) {
    return lineMapper.countByAddress(companyId,startCity,startAddress,endCity,endAddress,appKey);
  }

  @Override
  public List<Line> findByAddress(Long companyId, String startCity, String startAddress,
      String endCity, String endAddress, String appKey) {
    return lineMapper.findByAddress(companyId,startCity,startAddress,endCity,endAddress,appKey);
  }

  @Override
  public List<Line> querylineByCity(String city, String appKey) {
    return lineMapper.querylineByCity("%" + city + "%",appKey);
  }

  @Override
  public List<Line> lineIsExist(String startCity, String startAddress, String endcity,
      String endAddress, String appKey) {
    return lineMapper.lineIsExist(
            StringUtils.isNotBlank(startCity) ? "%" + startCity + "%" : null,
            StringUtils.isNotBlank(startAddress) ? "%" + startAddress + "%" : null,
            StringUtils.isNotBlank(endcity) ? "%" + endcity + "%" : null,
            StringUtils.isNotBlank(endAddress) ? "%" + endAddress + "%" : null,appKey);
  }

  @Override
  public List<Line> findLineByName(String name, String appKey, Long companyId) {
    return lineMapper.findLineByName(name,appKey,companyId);
  }


}
